package com.situ.system.employee.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.system.employee.model.EmployeeModel;
import com.situ.system.employee.service.EmployeeService;
import com.situ.system.util.FmtRequest;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import com.sun.swing.internal.plaf.metal.resources.metal;

public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// 接受请求 获取参数 封装对象 调用方法 返回结果
	public EmployeeServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		System.out.println(action);
		List<EmployeeModel> list = null;
		Object res = "";
		boolean f = true;
		switch (action) {
		case "reg":
			res = reg(request);
			break;
		case "login":
			res = login(request);
			break;
		case "huixian":
			f = false;
			list = huixian(request);
			FmtRequest.write(response.getWriter(), list);
			break;
		case "del":
			res = del(request);
			break;
		case "select":
			f = false;
			list = select(request);
			FmtRequest.write(response.getWriter(), list);
			break;
		case "get":
			res = get(request);
			break;
		case "udp":
			res = udp(request);
			break;
		case "page":
			res = page(request);
			break;
		case "udpass":
			res = udpass(request);
			break;
		case "updDept":
			res = updDept(request);
		case "goLogout":
			request.getSession().removeAttribute("user");
			response.sendRedirect(request.getContextPath() + "/web/login.jsp");
			return;
		default:
			break;
		}
		if (f) {
			FmtRequest.write(response.getWriter(), res);
		}
	}

	private EmployeeService service = new EmployeeService();

	private String updDept(HttpServletRequest request) {
		String id = request.getParameter("code");
		String codeDept = request.getParameter("codeDept");
		EmployeeModel employeeModel = new EmployeeModel();
		employeeModel.setId(id);
		employeeModel.setD_id(codeDept);
//		System.out.println(codeDept);
//		System.out.println(employeeModel);
		return service.updDept(employeeModel);
	}

	private String udpass(HttpServletRequest request) {
		String id = request.getParameter("code");
		String pass = request.getParameter("pass");
		EmployeeModel employeeModel = new EmployeeModel();
		employeeModel.setId(id);
		employeeModel.setPassword(pass);
		System.out.println(employeeModel);
		return service.updatepass(employeeModel);
	}

	private Map<String, Object> page(HttpServletRequest request) {
		EmployeeModel model = new EmployeeModel();
		String id = request.getParameter("bianhao");
		String name = request.getParameter("name");
		model.setId(id);
		model.setName(name);
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageLimit = Integer.parseInt(request.getParameter("pageLimit"));
		model.setPageIndex(pageIndex);
		model.setPageLimit(pageLimit);
		model.setPageOn(true);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return map;
	}

	private String udp(HttpServletRequest request) {
		String id = request.getParameter("code");
		String pass = request.getParameter("pass");
		String name = request.getParameter("name");
		EmployeeModel employeeModel = new EmployeeModel();
		employeeModel.setId(id);
		employeeModel.setPassword(pass);
		employeeModel.setName(name);
		return service.update(employeeModel);
	}

	private List<EmployeeModel> select(HttpServletRequest request) {
		String id = request.getParameter("bianhao");
		String name = request.getParameter("name");
		EmployeeModel employeeModel = new EmployeeModel();
		employeeModel.setId(id);
		employeeModel.setName(name);
		return service.selectAll(employeeModel);
	}

	private EmployeeModel get(HttpServletRequest request) {
		String id = request.getParameter("code");
		EmployeeModel employeeModel = new EmployeeModel(id);
		return service.selectModel(employeeModel);
	}

	private String del(HttpServletRequest request) {
		String id = request.getParameter("id");
		EmployeeModel employeeModel = new EmployeeModel(id);
		return service.delete(employeeModel);
	}

	private List<EmployeeModel> huixian(HttpServletRequest request) {
		EmployeeModel employeeModel = new EmployeeModel();
		return service.selectAll(employeeModel);
	}

	private String login(HttpServletRequest request) {
		String code = request.getParameter("code");
		String pass = request.getParameter("pass");
		EmployeeModel employeeModel = new EmployeeModel(code, pass);
		String res = service.login(employeeModel);
		if ("1".equals(res)) {
			request.getSession().setAttribute("user", service.selectModel(employeeModel));
		}
		return res;
	}

	private String reg(HttpServletRequest request) {
		String code = request.getParameter("code");
		String pass = request.getParameter("pass");
		String name = request.getParameter("name");
		EmployeeModel employeeModel = new EmployeeModel(code, pass, name);
		return service.insert(employeeModel);
	}
}
